@using DNA.Mvc.Community.Controllers;
@model Post
@{
    var forum = (Forum)ViewData["Forum"];
    var context = WebSiteContext.Current;
    
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <input type="hidden" value="@(Model != null ? Model.IP : Context.Request.UserHostAddress)" name ="Post.IP" />
    <table style="width: 100%">
        <tr>
            <td colspan="2">
                @Html.ValidationSummary()
            </td>
        </tr>
        @if (ViewData["TitlePrefix"] == null)
        {
            <tr>
                <td style="width: 80px; padding: 5px; text-align: right;">
                    <label for="PostTitle">
                        @Html.Global("RES_TITLE"):</label>
                </td>
                <td>
                    @Ajax.Dna().TextBox("Post.Title").Value((ViewData["TitlePrefix"] != null ? ViewData["TitlePrefix"].ToString() : "") + (Model == null ? "" : Model.Title)).Width(400).GetHtml()
                </td>
            </tr>
        }
        else
        { <tr>
            <td colspan="2">
                <input type="hidden" value="@((ViewData["TitlePrefix"] != null ? ViewData["TitlePrefix"].ToString() : "") + (Model == null ? "" : Model.Title))" name="Post.Title" />
            </td>
        </tr>}
        <tr>
            <td colspan="2">
                <div>
                    @{
        Ajax.Dna().RichTextBox("Post.Body")
            .AllowHTMLEditing(false)
            .Options(opts => { opts.Mode = "source"; })
            .Resizable()
            .Height(300)
            .Value(Model != null ? Model.Body : (ViewData["Quote"] != null ? ViewData["Quote"].ToString() : Html.Global("forums", "WriteYourContent")))
            .ToolPanes(panes =>
            {
                panes.Clear();
                panes.Add().CustomTools(tb =>
                {
                    tb.Add().AddCombo(combo =>
                    {
                        combo.Items(items =>
                        {
                            items.AddValue(Html.Global("RES_QUOTE"), "[quote]{0}[/quote]");
                            items.AddValue(Html.Global("RES_HEAD")+" 1", "[h1]{0}[/h1]\r\n");
                            items.AddValue(Html.Global("RES_HEAD") + " 2", "[h2]{0}[/h2]\r\n");
                            items.AddValue(Html.Global("RES_HEAD") + " 3", "[h3]{0}[/h3]\r\n");
                            items.AddValue(Html.Global("RES_HEAD") + " 4", "[h4]{0}[/h4]\r\n");
                            items.AddValue(Html.Global("RES_HEAD") + " 5", "[h5]{0}[/h5]\r\n");
                        })
                        .Options(opts =>
                        {
                            opts.OnItemSelected = "$bbf(ui.value);";
                            //opts.OnItemSelected = "_insertFormat($(\"#Post_Body\"),ui.value);";
                        })
                        .DropDownList();
                    })
                    .AddIcon("d-rte-icon d-rte-bold", "$bbf('[b]{0}[/b]');", "")
                    .AddIcon("d-rte-icon d-rte-italic", "$bbf('[i]{0}[/i]');", "")
                    .AddIcon("d-rte-icon d-rte-underline", "$bbf('[u]{0}[/u]');", "")
                    .AddIcon("d-rte-icon d-rte-strikeThrough", "$bbf('[s]{0}[/s]');", "")
                    .AddSeparator()
                    .AddIcon("d-rte-icon d-rte-link", "$bbf('[url]{0}[/url]');", "")
                    .AddColorPicker("d-rte-icon d-rte-foreColor", "$bbf('[color='+cmd+']{0}[/color]');")
                    .AddCombo(combo =>
                    {
                        #region Add font size combo
                        combo.Items(items =>
                        {
                            using (var writer = new HtmlTextWriter(this.ViewContext.Writer))
                            {
                                items.AddValue("inherit").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: inherit;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("(inherited size)");
                                    writer.WriteEndTag("span");
                                });

                                items.AddValue("8pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: xx-small;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("1 (8pt)");
                                    writer.WriteEndTag("span");
                                });
                                items.AddValue("10pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: x-small;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("2 (10pt)");
                                    writer.WriteEndTag("span");
                                });
                                items.AddValue("12pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: small;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("3 (12pt)");
                                    writer.WriteEndTag("span");
                                });
                                items.AddValue("14pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: medium;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("4 (14pt)");
                                    writer.WriteEndTag("span");
                                });
                                items.AddValue("18pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: large;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("5 (18pt)");
                                    writer.WriteEndTag("span");
                                });
                                items.AddValue("24pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: x-large;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("6 (24pt)");
                                    writer.WriteEndTag("span");
                                });
                                items.AddValue("36pt").Template(() =>
                                {
                                    writer.WriteBeginTag("span");
                                    writer.WriteAttribute("style", "font-size: xx-large;");
                                    writer.Write(HtmlTextWriter.TagRightChar);
                                    writer.Write("7 (36pt)");
                                    writer.WriteEndTag("span");
                                });
                            }
                        }).Options(opts => { opts.OnItemSelected = "$bbf('[size='+$(this).val()+']{0}[/size]');"; })
                          .Select("inherit");
                        #endregion
                    })
                    .AddIcon("d-rte-icon d-rte-insertCode", "$bbf('[code]{0}[/code]');", "");
                    //.AddIcon("", "", "");
                });
            })
            .Render();
                    }
                </div>
            </td>
        </tr>
        @if (forum.AllowAttachment)
        {  
            <tr>
                <td colspan="2">
                    <div style="height: 20px;">
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: right; vertical-align: top; padding: 5px 0px;">
                    <label for="PostAttachment">
                        @Html.Global("forums", "Attachments"):
                    </label>
                </td>
                <td style="padding: 5px 0px;">
                    @FileUpload.GetHtml("PostAttachments", includeFormTag: false, addText: Html.Global("RES_ADD_MORE_FILES"))
                    @{ Ajax.RegisterStartupScript("$(\"#PostAttachments\").find(\"input[type='submit']\").hide();"); }
                    @{ Html.RegisterStartupScript("$('form').attr(\"enctype\",\"multipart/form-data\")"); }
                    @if ((Model != null) && Model.HasAttachments)
                    {
                        <input id="deletedAttachments" name="deletedAttachments" value="" type="hidden" />
                        <ul class="ui-helper-reset" style="margin-top: 15px;">
                            @foreach (var attach in Model.Attachments)
                            {
                                <li style="list-style: none;line-height:25px;" id="@("attch_" + attach.ID.ToString())">
                                    @Community.AttachmentLink(attach, new { style = "float:left;" })
                                    <a href="javascript:void(0);" onclick="$delAttach(@attach.ID);" style="margin-left:10px;">@Html.Global("RES_DELETE")</a></li>
                            }
                        </ul>
                    }
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <hr />
                </td>
            </tr>
        }
        <tr>
            <td colspan="2">
                <dl>
                    @if ((Model == null) && ViewData["ThreadPost"] == null)
                    {
                        if (context.IsAuthorized<PostController>("Pin"))
                        { 
                        <dd style="clear: left; padding: 3px;">
                            @Ajax.Dna().Checkbox("PinPost", Html.Global("forums", "PinThread"), false,
                       Url.Content("~/content/images/icon_accept_16.png"),
                       Url.Content("~/content/images/icon_block_16.png")).GetHtml()
                        </dd>
                        }

                        if (context.IsAuthorized<PostController>("Lock"))
                        {
                        <dd style="clear: left; padding: 3px;">
                            @Ajax.Dna().Checkbox("LockPost", Html.Global("forums", "LockThread"), false,
                           Url.Content("~/content/images/icon_accept_16.png"),
                           Url.Content("~/content/images/icon_block_16.png")).GetHtml()
                        </dd>
                        }
                    }
                    @if ((Model != null) && (Model.ID > 0))
                    {
                        <dd style="clear: left; padding: 3px;">
                            @{ 
                        bool isSubscribed = ViewBag.IsSubscribed != null ? ViewBag.IsSubscribed : false;
                            }
                            @Ajax.Dna().Checkbox("NotifyMe", Html.Global("forums", "NotifyMeWhenReply"), isSubscribed,
                        Url.Content("~/content/images/icon_accept_16.png"),
                        Url.Content("~/content/images/icon_block_16.png")).GetHtml()
                        </dd>
                    }
                    else
                    { 
                        <dd style="clear: left; padding: 3px;">
                            @Ajax.Dna().Checkbox("NotifyMe", Html.Global("forums", "NotifyMeWhenReply"), true,
                        Url.Content("~/content/images/icon_accept_16.png"),
                        Url.Content("~/content/images/icon_block_16.png")).GetHtml()
                        </dd>
                    }
                </dl>
            </td>
        </tr>
        <tr>
            <td style="padding: 10px; text-align: center;" colspan="2">
                @Ajax.Dna().Button().Submit().ChangeType(ButtonTypes.LinkButton).Images(Url.Content("~/Content/Themes/" + context.Web.Theme + "/images/ui-forum-post.png"), Url.Content("~/Content/Themes/" + context.Web.Theme + "/images/ui-forum-post-hover.png")).GetHtml()
            </td>
        </tr>
    </table>
}
<script type="text/javascript">
    function $bbf(_format) {
        $dna.insertFormat($("#Post_Body"), _format);
    }

    function $delAttach(_id) {
        var _del = $("#deletedAttachments").val();
        if (_del) $("#deletedAttachments").val(_del + "," + _id);
        else $("#deletedAttachments").val(_id);
        $("#attch_" + _id).hide();
    }
</script>
